package net.k3rnel.abugadro.gcodes;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.k3rnel.abugadro.ProjectsDB;
import net.k3rnel.abugadro.gcodes.beans.CodeChange.Code;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.ContentResolver;
import android.content.ContentValues;


public class SubversionFeedHandler extends DefaultHandler{

	private ContentResolver content;
	private ContentValues currentCodeChange;
	private StringBuilder builder;
	private int gcodeId;
	private int codeId;
	private boolean inEntry = false;
	
	// names of the XML tags
	static final String FEED = "feed";
	static final String ENTRY = "entry";
	static final String AUTHOR = "author";
	
	static final String UP_DATE = "updated";
	static final String ID = "id";
	static final String TITLE = "title";
	static final String CONTENT = "content";
	static final String AUTHOR_NAME = "name";
	
	public SubversionFeedHandler(ContentResolver content, int projectId) {
		this.content = content;
		this.gcodeId = projectId;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		builder.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		super.endElement(uri, localName, name);
		if (this.currentCodeChange != null){
			if (localName.equalsIgnoreCase(ID)&&inEntry){
				String id = builder.toString();
				id = id.substring(id.indexOf("svnchanges/basic/")+17);
				codeId = Integer.parseInt(id);
				currentCodeChange.put(Code.CODE_ID,id);
			} else if (localName.equalsIgnoreCase(TITLE)&&inEntry){
				currentCodeChange.put(Code.TITLE,builder.toString());
			} else if (localName.equalsIgnoreCase(CONTENT)&&inEntry){
				currentCodeChange.put(Code.CONTENT,builder.toString().replace("&lt;br/&gt;", ""));
			} else if (localName.equalsIgnoreCase(UP_DATE)&&inEntry){
				currentCodeChange.put(Code.UPDATED,builder.toString());
			} else if (localName.equalsIgnoreCase(AUTHOR_NAME)&&inEntry){
				currentCodeChange.put(Code.AUTHOR,builder.toString().replace(" ", "").replace("\n",""));
			} else if (localName.equalsIgnoreCase(ENTRY)&&inEntry){
				inEntry = false;
				if(ProjectsDB.getInstance().isCodeInDB(content, gcodeId, codeId)==0)
					content.insert(Code.CONTENT_URI,currentCodeChange);
				else
					content.update(Code.CONTENT_URI,currentCodeChange, Code.GCODE_ID +" = "+gcodeId +" AND " +
							Code.CODE_ID + " = "+codeId, null);	
				currentCodeChange = new ContentValues();
			}else if (localName.equalsIgnoreCase(FEED)){
				ContentValues cv = new ContentValues();
				cv.put(Code.GCODE_ID,gcodeId);
				cv.put(Code.CODE_ID,codeId);
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // set the format to sql date time
				Date date = new Date();
				cv.put(Code.UPDATED, dateFormat.format(date));
				content.update(Code.CONTENT_URI,cv,Code.GCODE_ID + "='" + gcodeId + "' AND "+Code.CODE_ID+"='"+codeId+"'",null);
			}
			builder.setLength(0);	
		}
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, name, attributes);
		if(localName.equalsIgnoreCase(FEED)){
			ContentValues cv = new ContentValues();
			cv.put(Code.GCODE_ID,gcodeId);
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // set the format to sql date time
			Date date = new Date();
			cv.put(Code.UPDATED, dateFormat.format(date));
			content.update(Code.CONTENT_URI,cv,Code.GCODE_ID + "='" + gcodeId + "' AND "+Code.CODE_ID+"= '"+codeId+"'",null);
		}else if (localName.equalsIgnoreCase(ENTRY)){
			inEntry = true;
			this.currentCodeChange = new ContentValues();
			currentCodeChange.put(Code.GCODE_ID,gcodeId);
			currentCodeChange.put(Code.CODE_ID,codeId);
		}
	}
}

